﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    class AB45Report : BaseReport
    {
        #region ----------------PROPERTIES----------------------------------
        byte[] b;
        string MultiRowTable = "WASTGA;WASTPASS;BASTGA;GASTGA;WTLEIST;BASTKE;";
        int WarpBeamEnds11 = 0;
        int TotalEnds = 0;
        int TotalSelvage = 0;
        string ABPAUFFKRS;
        string ABPAUFFLFD;
        string ABPAUFFPOS;
        string DocType;
        string CellName = "";
        string CellValue = "";
        string strCompany;
        string orderComments = "备注：";
        string strExcelFile;
        double Warpdensity = 1.0;
        double Weftdensity = 1.0;

        double Totalends = 0;
        double WeftEnds = 1.0;
        double Jdense = 0.0;
        double Wdense = 0.0;
        double TotalDense = 0.0;
        BaseFunction m_gm = new BaseFunction();
        #endregion
        public AB45Report()
            : base()
        {
        }
        public AB45Report(Dictionary<string, object> dicParamater)
            : base(dicParamater)
        {
            ABPAUFFKRS = dicParamater["ABPAUFKRS"].ToString().Trim();
            ABPAUFFLFD = dicParamater["ABPAUFLFD"].ToString().Trim();
            ABPAUFFPOS = dicParamater["ABPAUFPOS"].ToString().Trim();
            DocType = dicParamater["DocType"].ToString().Trim();
            strCompany = dicParamater["Company"].ToString().Trim();
            strExcelFile = dicParamater["ExcelFile"].ToString().Trim();
        }
        #region------------------------------Function------------------------


        private string Byte2String(byte[] arrByte)
        {

            StringBuilder sb = new StringBuilder();

            foreach (byte b in arrByte)
            {

                sb.Append(b > 15 ? Convert.ToString(b, 16) : '0' + Convert.ToString(b, 16));

            }

            return sb.ToString();

        }
        private void FillInWarpYarnWS31(string TgaType, DataTable dtBastga)
        {
            string YarnArticle;
            string YarnVar;
            string YarnColor;
            string Yarn;
            string ProcessedYarn;
            DataTable dtBastke = null;
            string KART = "";
            string KVAR = "";
            string KFARBE = "";
            string KANZ = "";

            dtBastke = dsSource.Tables["WASTKE"];
            KART = "WAKKART";
            KVAR = "WAKKVAR";
            KFARBE = "WAKKFARBE";
            KANZ = "WAKEAFAKTR";

            #region WarpYarn
            var query = from dwBastga in dtBastga.AsEnumerable()
                        let BeamArticle = dwBastga.Field<string>("BAGART") + dwBastga.Field<string>("BAGVAR") + dwBastga.Field<string>("BAGFARBE")
                        join dWBastke in dtBastke.AsEnumerable()
                       on BeamArticle equals dWBastke.Field<string>(KART) + dWBastke.Field<string>(KVAR) + dWBastke.Field<string>(KFARBE)
                        group new { dwBastga, dWBastke } by new
                        {
                            YarnPos = dwBastga["BAGPOS"],
                            YarnArticle = dwBastga["BAGGART"],
                            YarnVar = dwBastga["BAGGVAR"],
                            YarnColor = dwBastga["BAGGFARBE"]
                        }
                            into grp
                            select new
                            {
                                Yarn = grp.Key,
                                TtlBAGKGNET = grp.Sum(g => g.dwBastga.Field<decimal>("BAGKGNET") * g.dWBastke.Field<decimal>(KANZ)),
                                TtlBAGKGBRUT = grp.Sum(g => g.dwBastga.Field<decimal>("BAGKGBRUT") * g.dWBastke.Field<decimal>(KANZ)),
                                TtlEnds = grp.Sum(g => g.dwBastga.Field<decimal>("BAGFDZ")),
                                MaxWastage = grp.Max(g => g.dwBastga.Field<decimal>("BAGABF")),
                            };
            #endregion
            int YarnNum = 0;
            string YarnPosition = "";
            Queue CellQ = new Queue();
            string YarnBase = "";
            string Yarndes = "";
            foreach (var varYarn in query)
            {
                YarnNum++;
                //YarnPosition = Right("00" + YarnNum.ToString(), 2);
                YarnPosition = varYarn.Yarn.YarnPos.ToString();
                Yarn = varYarn.Yarn.YarnArticle.ToString().Trim() + "/" + varYarn.Yarn.YarnVar.ToString().Trim() + "/" + varYarn.Yarn.YarnColor.ToString().Trim();
                YarnBase = varYarn.Yarn.YarnArticle.ToString().Trim() + "/" + varYarn.Yarn.YarnVar.ToString().Trim();
                Yarn = GetGB2312Value(Yarn);
                CellQ.Enqueue("WarpYarn" + YarnPosition);
                CellQ.Enqueue(Yarn.ToString());
                CellQ.Enqueue("WarpYarnDes" + YarnPosition);
                CellQ.Enqueue(GetYarnDes(dsSource.Tables["GSCOLOR"], Yarn));
                string TtlBAGKGNET = varYarn.TtlBAGKGNET.ToString();
                string TtlBAGKGBRUT = varYarn.TtlBAGKGBRUT.ToString();
                string TtlEnds = varYarn.TtlEnds.ToString();
                string MaxWastage = varYarn.MaxWastage.ToString();
                CellQ.Enqueue("WarpYarnNet" + YarnPosition);
                CellQ.Enqueue(TtlBAGKGNET);
                CellQ.Enqueue("WarpYarnGross" + YarnPosition);
                CellQ.Enqueue(TtlBAGKGBRUT);
                CellQ.Enqueue("WarpYarnEnds" + YarnPosition);
                CellQ.Enqueue(TtlEnds);
                CellQ.Enqueue("MaxWastage" + YarnPosition);
                CellQ.Enqueue(MaxWastage);
            }
            FillInCellbyQueue(CellQ);
            string CellName = "";
            string Cellvalue = "";
            CellName = "Jdense";
            Cellvalue = Jdense.ToString();
            exAgent.SetCellValue(CellName, Cellvalue, true);

        }

        private void HideNullRow()
        {
            try
            {
                int HidedRows = 0;
                int rowstart = 1;
                int rowend = 1;
                int col1, col2, col3;

                var val1 = exAgent.GetCellValue(1, 1);
                var val2 = exAgent.GetCellValue(1, 1);
                var val3 = exAgent.GetCellValue(1, 1);
                //Case 1
                rowstart = 30; rowend = 51; col1 = 3;
                for (int Rowi = rowstart; Rowi <= rowend; Rowi++)
                {
                    val1 = exAgent.GetRangeValue(Rowi, Rowi, 9, 38);
                    if ((string)val1 == "0")
                    {
                        HidedRows++;
                        exAgent.HideRows(Rowi, col1, Rowi, col1);
                    }
                }
                rowstart = 53; rowend = 56; col1 = 2; col2 = 3;
                for (int Rowi = rowstart; Rowi <= rowend; Rowi++)
                {
                    val1 = exAgent.GetRangeValue(Rowi, Rowi, 9, 38);
                    if ((string)val1 == "0")

                        //.get_Value(Type.Missing);
                        if ((string)val1 == "0")
                        {
                            HidedRows++;
                            exAgent.HideRows(Rowi, col1, Rowi, col1);
                        }
                }
                rowstart = 341; rowend = 351; col1 = 17; col2 = 23;
                for (int Rowi = rowstart; Rowi <= rowend; Rowi++)
                {
                    val1 = exAgent.GetRangeValue(Rowi, Rowi, 3, 4);
                    val2 = exAgent.GetRangeValue(Rowi, Rowi, 34, 35);
                    if ((string)val1 == "0" && (string)val2 == "0")
                    {
                        HidedRows++;
                        exAgent.HideRows(Rowi, col1, Rowi, col2);
                    }
                }
                int InsertedRows = HidedRows - 18;
                if (HidedRows > 0)
                {
                    rowstart = 359;
                    exAgent.InsertRows(rowstart, HidedRows - 18);
                }
                rowstart = 433 + InsertedRows; rowend = 1800 + InsertedRows;
                for (int Rowi = rowstart; Rowi <= rowend; Rowi++)
                {
                    val1 = exAgent.GetRangeValue(Rowi, Rowi, 8, 34);
                    if ((string)val1 == "0")
                    {
                        if (Rowi == rowstart)
                            Rowi = Rowi - 1;
                        exAgent.HideRows(Rowi, col1, rowend, col1);
                        Rowi = rowend;
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void FillInOrderHead(DataTable dtSource)
        {
            try
            {
                Queue CellQueue = new Queue();
                CellQueue.Clear();
                int i = 0;
                foreach (DataRow dwSource in dtSource.Rows)
                {
                    if (i > 0) return;
                    i++;
                    CellQueue.Enqueue("ResponseStaff");
                    CellQueue.Enqueue("HRNNAME");
                    CellQueue.Enqueue("Audit");
                    CellQueue.Enqueue("HRNNAME");
                    CellQueue.Enqueue("Telphone");
                    CellQueue.Enqueue("HRDTELH");
                    CellQueue.Enqueue("Fax");
                    CellQueue.Enqueue("HRDFAX");
                    CellQueue.Enqueue("OrderDate");
                    CellQueue.Enqueue("ABKAEDAT");
                    CellQueue.Enqueue("ABKKDAUFTNR");
                    CellQueue.Enqueue("ABKKDAUFTNR");
                    FillInCellbyQueue(CellQueue, dwSource);
                    orderComments += dwSource["ABKAUFTTXT1"].ToString();
                    orderComments += dwSource["ABKAUFTTXT2"].ToString() + Environment.NewLine;
                }
                CellName = "OrderComments";
                exAgent.SetCellValue(CellName, orderComments, true);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void FillInABFOLGE(DataTable dtSource)
        {

            Queue CellQueue = new Queue();
            CellQueue.Clear();

            foreach (DataRow dwSource in dtSource.Rows)
            {
                CellName = "Producer";
                CellValue = dwSource["LSNAME1"].ToString();
                exAgent.SetCellValue(CellName, CellValue, true);
            }
            CellName = "OrderComments";
            exAgent.SetCellValue(CellName, orderComments, true);
        }

        private void FillInABAUFM(DataTable dtSource)
        {

            Queue CellQueue = new Queue();
            CellQueue.Clear();

            foreach (DataRow dwSource in dtSource.Rows)
            {

                CellName = "WarpArticle";
                CellValue = dwSource["ABPART"].ToString().Trim() + "-" + dwSource["ABPFARBE"].ToString().Trim() + "-" + dwSource["ABPVAR"].ToString().Trim();
                exAgent.SetCellValue(CellName, CellValue, true);
                CellQueue.Enqueue("WeaveFty");
                CellQueue.Enqueue("LSNAME1");
                CellQueue.Enqueue("LedgeEnds");
                CellQueue.Enqueue("TotalSelvageEnds");
                CellQueue.Enqueue("ReserveWeaveOrder");
                CellQueue.Enqueue("ReserveWeaveOrder");
                FillInCellbyQueue(CellQueue, dwSource);
                Totalends += System.Convert.ToInt16(dwSource["TotalSelvageEnds"].ToString());
            }
            CellName = "Totalends";
            exAgent.SetCellValue(CellName, Totalends, true);
        }
        private void FillInOrderPos(DataTable dtSource)
        {

            Queue CellQueue = new Queue();
            CellQueue.Clear();

            foreach (DataRow dwSource in dtSource.Rows)
            {
                CellName = "ABOrder";
                CellValue = dwSource["ABPAUFKRS"].ToString() + "/" + dwSource["ABPAUFLFD"].ToString() + "/" + dwSource["ABPAUFPOS"].ToString();
                exAgent.SetCellValue(CellName, CellValue, true);
                CellQueue.Enqueue("SendingDate");
                CellQueue.Enqueue("ABPSTARTDAT");
                CellQueue.Enqueue("DelDate");
                CellQueue.Enqueue("ABPENDDAT");
                CellQueue.Enqueue("WarpEnds");
                CellQueue.Enqueue("BSFDZ");
                Totalends += System.Convert.ToInt16(dwSource["BSFDZ"].ToString());
                FillInCellbyQueue(CellQueue, dwSource);
                orderComments += dwSource["COMMENTS"].ToString();
            }
            CellName = "Totalends";
            exAgent.SetCellValue(CellName, Totalends, true);
            CellName = "OrderComments";
            exAgent.SetCellValue(CellName, orderComments, true); 
        }
        private void FillInBABST(DataTable dtSource)
        {

            Queue CellQueue = new Queue();
            CellQueue.Clear();
            string BeamKrs = "_SZ";
            string BeamNum = "000";
            string BeamType = "BeamType";
            string BeamL = "BeamL";
            int Beami = 0;
            int TotalQty = 0;
            foreach (DataRow dwSource in dtSource.Rows)
            {
                Beami++;
                //BMBAUMKRS,BMBAUMLFD,BMKETTTYP,BMSOLLLG
                BeamNum = Right("000" + Beami.ToString(), 3);
                string BMBAUMKRS = dwSource["BMBAUMKRS"].ToString();
                string BMBAUMLFD = dwSource["BMBAUMLFD"].ToString();
                string BMKETTTYP = dwSource["BMKETTTYP"].ToString();
                string BMSOLLLG = dwSource["BMSOLLLG"].ToString();
                TotalQty += System.Convert.ToInt16(BMSOLLLG);
                CellName = BeamKrs + BeamNum;
                CellValue = BMBAUMKRS + BMBAUMLFD;
                exAgent.SetCellValue(CellName, CellValue, true);
                CellName = BeamType + BeamNum;
                CellValue = BMKETTTYP;
                exAgent.SetCellValue(CellName, CellValue, true);
                CellName = BeamL + BeamNum;
                CellValue = BMSOLLLG;
                exAgent.SetCellValue(CellName, CellValue, true);
            }

            exAgent.SetCellValue("Totalbeams", Beami, true);
            exAgent.SetCellValue("TotalMeter", TotalQty, true);
        }
        private void FillInSelvageYarn(DataTable dtSource, int lines)
        {
            int start = 40 + lines;
            foreach (DataRow dwWASTGA in dtSource.Rows)
            {
                start++;
                //		纱线编码								纱线成分															颜色/描述										头份			需求kg																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																										
                string art = dwWASTGA["WAGGART"].ToString().Trim();
                string var = dwWASTGA["WAGGVAR"].ToString().Trim();
                string color= dwWASTGA["WAGGFARBE"].ToString().Trim();
                string ends = dwWASTGA["WAGFDZ"].ToString().Trim();
                double rkg = System.Convert.ToDouble( dwWASTGA["WAGKGBRUT"].ToString().Trim());
                double len =System.Convert.ToDouble( dsSource.Tables["ABPOS"].Rows[0]["ABPSOLLMENGE1"].ToString());
                // Composition GSCBEZ
                string Composition =m_gm.Convert8859P1ToGB2312( dwWASTGA["Composition"].ToString().Trim());
                string GSCBEZ =m_gm.Convert8859P1ToGB2312( dwWASTGA["GSCBEZ"].ToString().Trim());
                len = len * 0.01;
                rkg = rkg * 0.001 * 0.01;
                rkg = len * rkg;
                exAgent.SetCellValue(start, 3, art+"/"+var, true);
                exAgent.SetCellValue(start, 11, Composition, true);
                exAgent.SetCellValue(start, 26, color, true);
                exAgent.SetCellValue(start, 29, GSCBEZ, true);
                exAgent.SetCellValue(start, 36, ends, true);
                exAgent.SetCellValue(start, 39, "边纱", true);
            }
        }

        private int FillInBASTGA(DataTable dtSource)
        {

            Queue CellQueue = new Queue();
            CellQueue.Clear();

            string YarnNum = "000";
            int Yarni = 0;
            var querys = from dwRow in dtSource.AsEnumerable()
                         orderby dwRow["BAGPOS"].ToString()
                         select dwRow;
            foreach (DataRow dwSource in querys)
            {
                Yarni++;
                //GAART,   GAVAR,GAFARBE,GAKG1,GAKG3,ZUPARTIE,GASPUL, GPSPPART,LSNAME1
                //BAGGART,BAGGVAR,BAGGFARBE,GSCBEZ,BAGFDZ,Composition
                YarnNum = Right("000" + Yarni.ToString(), 3);
                string GAART = dwSource["BAGGART"].ToString().Trim();
                string GAVAR = dwSource["BAGGVAR"].ToString().Trim();
                string GAFARBE = dwSource["BAGGFARBE"].ToString().Trim();
                string GSCBEZ = m_gm.Convert8859P1ToGB2312(dwSource["GSCBEZ"].ToString()).Trim();
                //string GAKG1 = dwSource["GAKG1"].ToString();
                //string GAKG3 = dwSource["GAKG3"].ToString();
                //string ZUPARTIE = dwSource["ZUPARTIE"].ToString();
                string GASPUL = dwSource["BAGFDZ"].ToString().Trim();
                //string GPSPPART = dwSource["GPSPPART"].ToString();
                //string LSNAME1 = dwSource["LSNAME1"].ToString();
                //string LOLBLP = dwSource["LOLBLP"].ToString();
                string Composition = m_gm.Convert8859P1ToGB2312(dwSource["Composition"].ToString()).Trim();

                CellName = "WARPY" + YarnNum;
                CellValue = GAART + "/" + GAVAR;
                exAgent.SetCellValue(CellName, CellValue, true);
                CellName = "WARPYCom" + YarnNum;
                CellValue = Composition;
                exAgent.SetCellValue(CellName, CellValue, true);
                CellName = "WARPYColor" + YarnNum;
                CellValue = GAFARBE;
                exAgent.SetCellValue(CellName, CellValue, true);
                CellName = "WARPYColorDes" + YarnNum;
                CellValue = GSCBEZ;
                exAgent.SetCellValue(CellName, CellValue, true);
                CellName = "WARPYEnds" + YarnNum;
                CellValue = GASPUL;
                exAgent.SetCellValue(CellName, CellValue, true);
                DataRow rowGAAUF = null;
                var query = from dwGAAUF in dsSource.Tables["GAAUF"].AsEnumerable()
                            where dwGAAUF["GAART"].ToString().Trim() == GAART.Trim()
                            & dwGAAUF["GAVAR"].ToString().Trim() == GAVAR.Trim()
                            & dwGAAUF["GAFARBE"].ToString().Trim() == GAFARBE.Trim()
                            select dwGAAUF;
                foreach (DataRow row in query)
                {
                    rowGAAUF = row;
                }
                if (rowGAAUF != null)
                {
                    //WARPYSPL010                WARPYDYL001
                    //,GPSPPART,GPFBPART
                    CellName = "WARPYSPL" + YarnNum;
                    CellValue = rowGAAUF["GPSPPART"].ToString();
                    exAgent.SetCellValue(CellName, CellValue, true);
                    CellName = "WARPYDYL" + YarnNum;
                    CellValue = rowGAAUF["GPFBPART"].ToString();
                    exAgent.SetCellValue(CellName, CellValue, true);
                }
                /*
                 GAART, GAVAR,GAFARBE
                 
                 * CellName = "WARPYSupplier" + YarnNum;
                CellValue = LSNAME1;
                exAgent.SetCellValue(CellName, CellValue, true);
                CellName = "WARPYReq" + YarnNum;
                CellValue = GAKG1;
                exAgent.SetCellValue(CellName, CellValue, true);
                 CellName = "WARPYAllocate" + YarnNum;
                 CellValue = GAKG3;
                 exAgent.SetCellValue(CellName, CellValue, true);
                CellName = "WARPYWH" + YarnNum;
                CellValue = LOLBLP;
                exAgent.SetCellValue(CellName, CellValue, true); */
            }
            return Yarni;
        }
        private int FillInGAAUF(DataTable dtSource)
        {

            Queue CellQueue = new Queue();
            CellQueue.Clear();

            string YarnNum = "000";
            int Yarni = 0;
            foreach (DataRow dwSource in dtSource.Rows)
            {
                Yarni++;
                //GAART,   GAVAR,GAFARBE,GAKG1,GAKG3,ZUPARTIE,GASPUL, GPSPPART,LSNAME1
                //WARPYSPL010                WARPYDYL001

                YarnNum = Right("000" + Yarni.ToString(), 3);
                string GAART = dwSource["GAART"].ToString();
                string GAVAR = dwSource["GAVAR"].ToString();
                string GAFARBE = dwSource["GAFARBE"].ToString();
                string GSCBEZ = dwSource["GSCBEZ"].ToString();
                string GAKG1 = dwSource["GAKG1"].ToString();
                string GAKG3 = dwSource["GAKG3"].ToString();
                string ZUPARTIE = dwSource["ZUPARTIE"].ToString();
                string GASPUL = dwSource["GASPUL"].ToString();
                string GPSPPART = dwSource["GPSPPART"].ToString();
                string LSNAME1 = dwSource["LSNAME1"].ToString();
                string LOLBLP = dwSource["LOLBLP"].ToString();
                string Composition = dwSource["Composition"].ToString();

                CellName = "WARPY" + YarnNum;
                CellValue = GAART + "/" + GAVAR;
                exAgent.SetCellValue(CellName, CellValue, true);
                CellName = "WARPYCom" + YarnNum;
                CellValue = Composition;
                exAgent.SetCellValue(CellName, CellValue, true);
                CellName = "WARPYColor" + YarnNum;
                CellValue = GAFARBE;
                exAgent.SetCellValue(CellName, CellValue, true);
                CellName = "WARPYColorDes" + YarnNum;
                CellValue = GSCBEZ;
                exAgent.SetCellValue(CellName, CellValue, true);
                CellName = "WARPYEnds" + YarnNum;
                CellValue = GASPUL;
                exAgent.SetCellValue(CellName, CellValue, true);
                /*CellName = "WARPYSupplier" + YarnNum;
                CellValue = LSNAME1;
                exAgent.SetCellValue(CellName, CellValue, true);*/
                CellName = "WARPYReq" + YarnNum;
                CellValue = GAKG1;
                exAgent.SetCellValue(CellName, CellValue, true);
                /* CellName = "WARPYAllocate" + YarnNum;
                 CellValue = GAKG3;
                 exAgent.SetCellValue(CellName, CellValue, true);*/
                CellName = "WARPYWH" + YarnNum;
                CellValue = LOLBLP;
                exAgent.SetCellValue(CellName, CellValue, true); 
            }
            return Yarni;
        }
        #endregion
        public void GetReport(string fileType)
        {

            var query = from dwABPOS in dsSource.Tables["ABPOS"].AsEnumerable()
                        select dwABPOS;

            Queue CellQueue = new Queue();
            string value = "";
            string filename = "";
            string sheetName = "";
            string Standard = "PRODWARP";
            string filePath = @"C:\intex.w\Report\" + strExcelFile;
            filePath = strExcelFile;
            exAgent.Open(filePath);
            exAgent.Company = strCompany;
            exAgent.strExcelFile = strExcelFile;

            filename = @"C:\intex.w\Report\" + ABPAUFFKRS + ABPAUFFLFD + ".xls";
            sheetName = ABPAUFFKRS + "_" + ABPAUFFLFD + "_" + ABPAUFFPOS;
            exAgent.CreateNewSheet(filename, Standard, sheetName);
            CellName = "PrintDate";
            value = System.DateTime.Today.ToShortDateString();
            exAgent.SetCellValue(CellName, value, true);
            CellName = "PageNum";
            value = "1";
            exAgent.SetCellValue(CellName, value, true);
            try
            {
                FillInOrderHead(dsSource.Tables["ABKOPF"]);
                FillInOrderPos(dsSource.Tables["ABPOS"]);
                FillInABFOLGE(dsSource.Tables["ABFOLGE"]);
                FillInABAUFM(dsSource.Tables["ABAUFM"]);
                FillInBABST(dsSource.Tables["BABST"]);
                //int lines= FillInGAAUF(dsSource.Tables["GAAUF"]);
                int lines = FillInBASTGA(dsSource.Tables["BASTGA"]);
                FillInSelvageYarn(dsSource.Tables["WASTGA"], lines); ;
            }
            catch
            {}
            if (dsSource.Tables.Contains("ABPR"))
            {
                if (dsSource.Tables["ABPR"].Rows.Count > 0)
                {
                    DataRow dwABPR = dsSource.Tables["ABPR"].Rows[0];
                    double arwert = System.Convert.ToDouble(dwABPR["ARWERT"].ToString()) * 0.001;
                    exAgent.SetCellValue(5, 38, arwert);

                }
            }
            if (dsSource.Tables.Contains("VKPOS"))
            {
                if (dsSource.Tables["VKPOS"].Rows.Count > 0)
                {
                    DataRow dwVKPOS = dsSource.Tables["VKPOS"].Rows[0];
                    string VKPKDARTNR = dwVKPOS["VKPKDARTNR"].ToString();
                    VKPKDARTNR = m_gm.Convert8859P1ToGB2312(VKPKDARTNR);
                    exAgent.SetCellValue(14, 38, VKPKDARTNR);
                }
            }
            if (dsSource.Tables.Contains("WTGEW"))
            {
                if (dsSource.Tables["WTGEW"].Rows.Count > 0)
                {
                    DataRow dwWTGEW = dsSource.Tables["WTGEW"].Rows[0];
                    double WTGWBBRT = System.Convert.ToDouble(dwWTGEW["WTGWBBRT"].ToString()) * 0.1;

                    exAgent.SetCellValue(51, 5, WTGWBBRT);
                }
            }
            if (dsSource.Tables.Contains("ABPOS"))
            {
                if (dsSource.Tables["ABPOS"].Rows.Count > 0)
                {
                    DataRow dwABPOS = dsSource.Tables["ABPOS"].Rows[0];
                    string BSFCOMM = dwABPOS["BSFCOMM"].ToString();
                    //BSFCOMM = m_gm.Convert8859P1ToGB2312(BSFCOMM);
                    exAgent.SetCellValue(51, 16, BSFCOMM);
                }
            }
            string localFile = exAgent.FileName;
            if (exAgent != null)
            {
                exAgent.Save();
                exAgent.Close();
            }            
            //System.Diagnostics.Process.Start("EXCEL.EXE", localFile);
        }
    }
}
